// 在这里获取菜单并动态注册路由

import router from "@/router/index";
import { initDynamicRoutes } from "@/utils/utils";
import { getUserMenu } from "@/api/login/index";

const Menu = {
  state: {
    // 当前用户所能访问的菜单
    userMenu: JSON.parse(window.sessionStorage.getItem("UserMenu") || "{}"),

    // 这里存放当前导航列表
    now: JSON.parse(window.sessionStorage.getItem("CurrentMenu") || "{}")
  },

  mutations: {
    setnow(state, e) {
      state.now = state.userMenu[e];
      window.sessionStorage.setItem(
        "CurrentMenu",
        JSON.stringify(state.userMenu[e])
      );
    },

    //保存登录用户所拥有的菜单信息
    setUserMenu(state, e) {
      state.userMenu = e;
    },

    // 鸽棚信息管理导航栏 dovecoteNumber 所有鸽笼号
    modifyDovecote(state, dovecoteNumber) {
      const allMenu = dovecoteNumber.map(item => {
        return {
          id: "201",
          title: item,
          icon: "el-icon-s-order",
          children: [
            {
              id: "2011",
              title: "全部鸽笼",
              path: `/Dovecote/AllHouse/${item}`,
              icon: "el-icon-office-building"
            }
          ]
        };
        // {
        //         id: '201',
        //         title: 'A01',
        //         icon: 'el-icon-s-order',
        //         children: [
        //             {
        //                 id: '2011',
        //                 title: '全部鸽笼',
        //                 path: `/Dovecote/AllHouse/${item}`,
        //                 icon: 'el-icon-office-building'
        //             }
        //         ]
        // },
      });
      state.Dovecote.children.push;
    }
  },

  actions: {
    // 获取登录用户所能访问菜单
    saveUserMenuAndRouteData({ commit }) {
      return new Promise((resolve, reject) => {
        getUserMenu()
          .then(res => {
            console.log('菜单88',res);
            const { data } = res.data;
            // console.log('菜单',data)
            let userMenu = {};
            data.forEach(item => {
              userMenu[item.name] = item;
            });
            console.log(userMenu, "userMenu=====");
            userMenu.Device_Manage.childPermission.push({
              bottom: null,
              childPermission: [],
              component: "CameraManage",
              icon: "el-icon-camera",
              id: "123456",
              name: "CameraManage",
              path: "/Dervice/CameraManage",
              redirect: null,
              rights: [],
              title: "摄像头管理"
            });
            userMenu.Device_Manage.childPermission.push({
              bottom: null,
              childPermission: [],
              component: "LightManage",
              icon: "el-icon-sunny",
              id: "123456789",
              name: "LightManage",
              path: "/Dervice/LightManage",
              redirect: null,
              rights: [],
              title: "智能亮灯"
            });
            userMenu.Warn.childPermission[0].icon = "el-icon-warning-outline";
            userMenu.Warn.childPermission.push({
              bottom: null,
              childPermission: [],
              component: "WarnOnline",
              icon: "el-icon-camera",
              id: "12355667787",
              name: "WarnOnline",
              path: "/Warn/WarnOnline",
              redirect: null,
              rights: [],
              title: "算法监测"
            });
            commit("setUserMenu", userMenu);
            window.sessionStorage.setItem("UserMenu", JSON.stringify(userMenu));

            const routeArr = initDynamicRoutes(data, []);
            router.addRoutes(routeArr);
            // routeArr.forEach( item => {
            //     console.log(router);
            //     router.addRoute(item)
            // } )

            window.sessionStorage.setItem("RouteData", JSON.stringify(data));
            resolve(data);
          })
          .catch(err => {
            reject(err);
          });
      });
    }

    // setMenu(context,data) {
    //     function tree(context,data) {
    //         data.forEach( item  => {
    //             context.state[item.name].push({
    //                 id: item.id,
    //                 title: item.title,
    //                 path: item.path,
    //                 icon: item.icon,
    //                 children: []
    //             })
    //             if ( item.childPermission ) {
    //                 const childArr = tree( context.state[item.name].children, [] )
    //                 context.state[item.name].children = childArr
    //             }
    //         })
    //     }

    //     tree(context,data)

    //     console.log(Menu);
    // }
  }
};

export default Menu;
